package cn.bocweb.spinning.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.CompanyCollectBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.adapter.CompanyInfoAdapter;
import cn.bocweb.spinning.view.common.BaseActivity;
import de.greenrobot.event.EventBus;

public class CompanyInfoActivity extends BaseActivity {

    String id;
    boolean collect; // true为收藏, false为未收藏
    CompanyInfoAdapter adapter;
    UserPresenter presenter;

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.tabLayout)
    TabLayout tabLayout;
    @Bind(R.id.viewPager)
    ViewPager viewPager;
    @Bind(R.id.company_detail_collect)
    ImageView companyDetailCollect;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_company_info);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        presenter = new UserPresenterImp();

        initIntent();
        initToolbar();
        initCollect();
        initViewPager();
    }

    public void onEventMainThread(CompanyCollectBean bean) {
        if (bean.getReturn_code() == 0) {
            String toast = collect ? "取消收藏成功" : "收藏成功";

            DeviceUtil.MyToast(this, toast);

            collect = !collect;
            int collectIcon = collect ? R.mipmap.collected : R.mipmap.product_detail_collect;
            companyDetailCollect.setImageResource(collectIcon);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    private void initIntent() {
        Intent intent = getIntent();
        id = intent.getStringExtra(Constant.TO_COMPANY);
        int collectInt = Integer.parseInt(intent.getStringExtra(Constant.TO_COMPANY_COLLECT));
        collect = collectInt == 1 ? true : false;
    }

    private void initCollect() {
        int collectIcon = collect ? R.mipmap.collected : R.mipmap.product_detail_collect;
        companyDetailCollect.setImageResource(collectIcon);
    }

    private void initViewPager() {
        adapter = new CompanyInfoAdapter(getSupportFragmentManager(), id);

        viewPager.setAdapter(adapter);

        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.colorPrimary));
    }

    private void initToolbar() {
        toolbar.setTitle("企业认证");
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @OnClick(R.id.company_detail_collect)
    public void onClick() {
        UserBean.DataEntity user = new UserBean.DataEntity();
        user.readInfoFromLocal(this);
        int sendCollect = collect ? 2 : 1;
        presenter.getCompanyCollect(id, user.getUserid(), String.valueOf(sendCollect));
    }
}
